﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    //
    public class DSTAB50Data : BaseFunction
    {

        INTEXDBC INTEXDBC;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public DSTAB50Data()
            : base()
        {

        }
        public DSTAB50Data(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
        }
        public DSTAB50Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {

        }
        public DataSet GetQTY(DataRow dwABPOS)
        {
            DataSet setResult = new DataSet();
            DataSet setReturn = new DataSet();
            string TableName = "VKKOPF";
            string AUFKRS = dwABPOS["ABPAUFKRS"].ToString();
            string AUFLFD = dwABPOS["ABPAUFLFD"].ToString();

            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "select TRIM(ABPART) AS VKKORTU,SUM(ROUND(ABPSOLLMENGE5*1.087*0.01,2)) AS VKKKOMENGE ,TRIM(ABPLTVORL) AS VKKZUSATZ "
                    + " from abpos where abpaufkrs = '"+AUFKRS+"' AND ABPAUFLFD = '"+AUFLFD+"' "
                    + " GROUP BY ABPART,ABPLTVORL "
                    ;

                setResult = Connect2Proxy(orlReadCommand);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = TableName;
                    byte[] b;
                    string value;
                    foreach (DataRow dw in setResult.Tables[TableName].Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["VKKZUSATZ"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["VKKZUSATZ"] = value;
                    }
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet BuildData(DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwABKOPF = null;
            string VKPAUFKRS = "";
            string VKPAUFLFD = "";
            try
            {
                #region=======getreq=======
                Result = GetQTY(dwInput);
                Return.Merge(Result);
                #endregion
                DataRow rowtemp = null;
                #region=======ABKOPF=======
                rowtemp = INTEXDB.ABKOPF.NewRow();
                rowtemp["ABKMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["ABKAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["ABKAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryABKOPF(rowtemp);
                if (Result.Tables[0].Rows.Count == 0)
                    return Return;
                Return.Merge(Result);
                #endregion               
                #region=======VKPOS=======
                dwABKOPF = Result.Tables[0].Rows[0];
                string vk = dwABKOPF["ABKKDAUFTNR"].ToString();
                int indexs = vk.IndexOf("-");
                VKPAUFKRS = Left(vk.Substring(0, indexs) + "  ", 2);
                VKPAUFLFD = Right("000000" + vk.Substring(indexs + 1).Trim(), 6);
                rowtemp = INTEXDB.VKPOS.NewRow();
                rowtemp["VKPMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["VKPAUFKRS"] = VKPAUFKRS;
                rowtemp["VKPAUFLFD"] = VKPAUFLFD;
                Result = INTEXDBC.QueryVKPOS(rowtemp, 2);
                Return.Merge(Result);
                #endregion
                #region=======WDECK=======

                rowtemp = INTEXDB.WDECK.NewRow();
                rowtemp["WDMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["WDAUFKRS"] = VKPAUFKRS;
                rowtemp["WDAUFLFD"] = VKPAUFLFD;
                Result = INTEXDBC.QueryWDECK(rowtemp, 4);
                Return.Merge(Result);
                #endregion
                #region=======WAFARBEN=======

                rowtemp = INTEXDB.WDECK.NewRow();
                rowtemp["WDMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["WDAUFKRS"] = VKPAUFKRS;
                rowtemp["WDAUFLFD"] = VKPAUFLFD;
                Result = INTEXDBC.QueryWAFARBEN(rowtemp, 4);
                Return.Merge(Result);
                #endregion
                #region=======WASTAMM=======

                rowtemp = INTEXDB.WDECK.NewRow();
                rowtemp["WDMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["WDAUFKRS"] = VKPAUFKRS;
                rowtemp["WDAUFLFD"] = VKPAUFLFD;
                Result = INTEXDBC.QueryWASTAMM(rowtemp, 4);
                Return.Merge(Result);
                #endregion
                #region=======WASTGA=======

                rowtemp = INTEXDB.WDECK.NewRow();
                rowtemp["WDMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["WDAUFKRS"] = VKPAUFKRS;
                rowtemp["WDAUFLFD"] = VKPAUFLFD;
                Result = INTEXDBC.QueryWASTGA(rowtemp, 4);
                Return.Merge(Result);
                #endregion
                #region=======BASTGA=======

                rowtemp = INTEXDB.WDECK.NewRow();
                rowtemp["WDMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["WDAUFKRS"] = VKPAUFKRS;
                rowtemp["WDAUFLFD"] = VKPAUFLFD;
                Result = INTEXDBC.QueryBASTGA(rowtemp, 4);
                Return.Merge(Result);
                #endregion
                #region=======ABPOS=======

                rowtemp = INTEXDB.ABPOS.NewRow();
                rowtemp["ABPMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["ABPAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["ABPAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryABPOS(rowtemp, 2);
                Return.Merge(Result);
                #endregion
                #region=======ABPR=======

                rowtemp = INTEXDB.ABPR.NewRow();
                rowtemp["ARMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["ARAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["ARAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryABPR(rowtemp, 4);
                Return.Merge(Result);

                #endregion
                #region=======ABFOLGE=======
                rowtemp = INTEXDB.ABFOLGE.NewRow();
                rowtemp["ABFMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["ABFAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["ABFAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryABFOLGE(rowtemp, 3);
                Return.Merge(Result);
                #endregion
                #region=======GAAUF=======
                rowtemp = INTEXDB.GAAUF.NewRow();
                rowtemp["GAMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["GAAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["GAAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryGAAUF(rowtemp, 4);
                Return.Merge(Result);

                #endregion
                #region=======GSDAT=======
                rowtemp = INTEXDB.GAAUF.NewRow();
                rowtemp["GAMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["GAAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["GAAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryGSDAT(rowtemp, 4);
                Return.Merge(Result);
                #endregion
                #region=======GSMATK=======
                rowtemp = INTEXDB.GAAUF.NewRow();
                rowtemp["GAMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["GAAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["GAAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryGSMATK(rowtemp, 4);
                Return.Merge(Result);
                #endregion
                #region=======GSCOLOR=======
                rowtemp = INTEXDB.GAAUF.NewRow();
                rowtemp["GAMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["GAAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["GAAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryGSCOLOR(rowtemp, 4);
                Return.Merge(Result);

                #endregion
                #region=======LIEFDAT=======
                rowtemp = INTEXDB.ABFOLGE.NewRow();
                rowtemp["ABFMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["ABFAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["ABFAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryLIEFDAT(rowtemp, 4);
                Return.Merge(Result);
                #endregion                 
                #region=======ZUTEI=======
                rowtemp = INTEXDB.ZUTEI.NewRow();
                rowtemp["ZUMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["ZUAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["ZUAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryZUTEI(rowtemp, 4);
                Return.Merge(Result);
                #endregion
                #region=======GPDAT=======
                rowtemp = INTEXDB.ZUTEI.NewRow();
                rowtemp["ZUMANDANT"] = dwInput["ABPMANDANT"];
                rowtemp["ZUAUFKRS"] = dwInput["ABPAUFKRS"];
                rowtemp["ZUAUFLFD"] = dwInput["ABPAUFLFD"];
                Result = INTEXDBC.QueryGPDAT(rowtemp, 4);
                Return.Merge(Result);
                #endregion
                
                foreach (DataRow row in Return.Tables["ABPOS"].Rows)
                {
                    string order = "";
                    string remark = "";
                    order = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + "   ";
                    remark += GetCommentsFromTXTIND("AB3", order).Trim();
                    order = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + row["ABPAUFPOS"].ToString();
                    remark += GetCommentsFromTXTIND("ABP", order).Trim();
                    row["ABPFILLER1"] = remark;
                    
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }

    }
}